package com.cwnu.onlinemall.controller;

import com.cwnu.onlinemall.pojo.MallAccount;
import com.cwnu.onlinemall.pojo.ServerResult;
import com.cwnu.onlinemall.service.accountService;
import com.cwnu.onlinemall.service.masterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class accountController {
    @Autowired
    accountService accountService;
    @RequestMapping("/account/selectAll")
    public ServerResult selectAll(){
        return new ServerResult(0,"查询成功", accountService.selectAll());
    }

    @RequestMapping("/account/selectById")
    public ServerResult selectById(Integer id){
        return new ServerResult(0,"查询成功", accountService.selectById(id));
    }


    @RequestMapping("/account/deleteaccountById")
    public ServerResult deleteaccountById(Integer id){
        Integer row = accountService.deleteaccountById(id);
        if(row >= 1){
            return new ServerResult(0,"删除成功",row);//返回数据是受影响的行数
        }
        else{
            return new ServerResult(1,"删除失败",null);
        }
    }

    @RequestMapping("/account/insertaccountById")
    public ServerResult insertaccountById(MallAccount mallAccount){
        Integer row = accountService.insertaccount(mallAccount);
        if(row >= 1){
            return new ServerResult(0,"插入成功",mallAccount);//返回数据是受影响的行数
        }
        else{
            return new ServerResult(1,"插入失败",null);
        }
    }

    @RequestMapping("/account/updateaccount")
    public ServerResult updateaccount(MallAccount mallAccount){
        Integer row = accountService.updateaccount(mallAccount);
        if(row >= 1){
            return new ServerResult(0,"修改成功",row);//返回数据是受影响的行数
        }
        else{
            return new ServerResult(1,"修改失败",null);
        }
    }

    @RequestMapping("/account/login")
    public ServerResult login(MallAccount mallAccount){
        List<MallAccount> mallAccount1 = accountService.login(mallAccount);
        MallAccount account = mallAccount1.get(0);
        if (account.getAccountAccount().equals(mallAccount.getAccountAccount())&& account.getAccountPassword().equals(mallAccount.getAccountPassword())){
            return new ServerResult(0,"登录成功", account);
        }else {
            return new ServerResult(1,"登录失败",null);
        }
    }

    @RequestMapping("/account/selectByaccount")
    public ServerResult selectByaccount(MallAccount mallAccount){
        List<MallAccount> mallAccount1 = accountService.selectByaccount(mallAccount);
        MallAccount account = mallAccount1.get(0);
        if (account.getAccountAccount().equals(mallAccount.getAccountAccount())){
            return new ServerResult(0,"查询成功", account);
        }else {
            return new ServerResult(1,"查询失败",null);
        }
    }

}
